package com.thinkcube.edutainment.fragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Fragment;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.thinkcube.edutainment.EdutainmentConstant;
import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.ReachabilityTest;
import com.thinkcube.edutainment.adapters.ForumPostAdapter;
import com.thinkcube.edutainment.adapters.ForumPostDetailAdapter;
import com.thinkcube.edutainment.adapters.OtherForumPostAdapter;
import com.thinkcube.edutainment.client.ForumClient;
import com.thinkcube.edutainment.client.UserClient;
import com.thinkcube.edutainment.domain.OAuth;
import com.thinkcube.edutainment.domain.PostItem;
import com.thinkcube.edutainment.domain.data.CourseData;
import com.thinkcube.edutainment.domain.data.PostData;
import com.thinkcube.edutainment.domain.dto.CourseDto;
import com.thinkcube.edutainment.domain.dto.OAuthDto;
import com.thinkcube.edutainment.service.SPreferences_;

@EFragment(R.layout.forum_detail_fragment)
public class ForumDetailFragment extends Fragment {

	CourseData courseData;

	PostData forumData;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments().containsKey("Course")) {
			courseData = getArguments().getParcelable("Course");
		}

		if (getArguments().containsKey("FourumId")) {
			forumData = getArguments().getParcelable("FourumId");
		}

	}

	@Bean
	OAuthDto oAuthDto;

	@RestService
	UserClient userClient;

	@RestService
	ForumClient forumClient;

	@ViewById(R.id.progressBar)
	ProgressBar progress;
	
	@ViewById(R.id.back_to_topic)
	Button backToTopic;

	@Pref
	SPreferences_ pref;

	@Bean
	ForumPostAdapter forumPostAdapter;
	@Bean
	OtherForumPostAdapter otherForumPostAdapter;

	@AfterViews
	void initUi() {
		gettoken();

	}

	@Trace
	@Background
	void gettoken() {
		oAuthDto.setAppId(EdutainmentConstant.APP_ID);
		oAuthDto.setAppSecret(EdutainmentConstant.APP_SCRET);

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					System.setProperty("http.keepAlive", "false");
					getForum(userClient.getRequestToken(oAuthDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});
	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}

	@Bean
	CourseDto courseDto;

	@Background
	void getForum(OAuth requestToken) {

		courseDto.setAuthToken(requestToken.getoAuth().getToken());

		// courseDto.setUserId(pref.getId().get());

		// courseDto.setCourseId(courseData.getCourseId());

		// courseDto.setForumId(forumData.getForumId());
		System.out.println("id "+forumData.getId());
		courseDto.setPostId(forumData.getId());
		// courseDto.setUserId("5289cf1e6bf841e116dd886d");

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					getForumUI(forumClient.getPost(courseDto
							.getMultiValuedMap()));

				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});

	}

	@Bean
	ForumPostDetailAdapter forumPostDetailAdapter;

	@ViewById(R.id.forum_detail_list)
	ListView forumDetailList;

	@ViewById(R.id.forum_detail_title)
	TextView forumDetailTitle;

	@ViewById(R.id.post_description)
	TextView postDescription;

	@Trace
	@UiThread
	void getForumUI(PostItem postItem) {

		// System.out.println(post.getForumData().getTitle());

		forumDetailTitle.setText(forumData.getBody());

		//postDescription.setText(postItem.getPostItemData().getPostData().);

		forumPostDetailAdapter.setPostData(postItem.getPostItemData().getClidPostDataList());

		progress.setVisibility(View.GONE);

		forumDetailList.setAdapter(forumPostDetailAdapter);

	}
	
	
	@Click(R.id.back_to_topic)
	void clickBackto()
	{
		getFragmentManager().popBackStack();
	}

}
